package com.igeek.collateral.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.collateral.entity.Collateral;
import com.igeek.collateral.entity.History;
import com.igeek.collateral.result.Result;
import com.igeek.collateral.service.IHistoryService;
import com.igeek.collateral.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * TODO
 *
 * @author chh
 * @since 2024/7/12
 **/
@RestController
@RequestMapping("/admin/history")
@Api(tags = "历史流程接口")
@Slf4j
public class HistoryController {

    @Autowired
    private IHistoryService iHistoryService;


    @PostMapping("add/in")
    @ApiOperation("添加历史记录（正常进库的）")
    public Result addI(@RequestBody History history, @RequestParam("inboundId") Integer inboundId)
    {
        try {
            boolean save = iHistoryService.addI(history,inboundId);
            if(save)
            {
                return new Result(true,"添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,"添加失败");

    }

    @PostMapping("add/out")
    @ApiOperation("添加历史记录（正常出库的）")
    public Result addO(@RequestBody History history, @RequestParam("outboundId") Integer outboundId)
    {
        try {
            boolean save = iHistoryService.addO(history,outboundId);
            if(save)
            {
                return new Result(true,"添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,"添加失败");

    }



    //分页查询
    @GetMapping("findPage")
    @ApiOperation("条件分页查询押品信息")
    public Result findPage(PageQueryVO vo){
        try {
            Page<History> page = iHistoryService.findPage(vo);
            if(page!=null){
                return new Result(true , "查询成功" , page);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false , "查询失败");
    }


    //删除操作，根据进库id删除
    @DeleteMapping("{id}")
    @ApiOperation("根据id删除进库信息")
    public Result delete (@PathVariable("id") Integer id)
    {
        try {
            boolean flag=iHistoryService.delete(id);
            if(flag)
            {
                return new Result(true,"删除成功");
            }
        } catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return new Result(false,"删除失败");
    }



    //新增押品的流程记录


}
